import {
  wxRequest
} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({

  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    showModal: false,
    pageNo: 1,
    hasNextPage: false,
    statusList: [{
      id: '',
      text: "全部"
    }, {
      id: 1,
      text: "待付款"
    }, {
      id: 2,
      text: "待收货"
    }, {
      id: 3,
      text: "已完成"
    }, {
      id: 40,
      text: "已取消"
    }],
    status: ''
  },
  onLoad(option) {
    this.setData({
      status: option.status || ''
    })
  },
  onShow: function() {
    this.getlist()
  },

  //tabl切换
  hadleTab: function(e) {
    var id = e.currentTarget.dataset.id
    this.setData({
      status: id,
      pageNo: 1
    })
    this.getlist()
  },


  // 获取订单
  getlist: function() {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/list?status=' + this.data.status + '&pageNo=' + this.data.pageNo,
      method: 'POST',
    }, (res) => {
      let list = res.list;
      if (this.data.pageNo !== 1) {
        list = this.data.list.concat(list);
      }

      list.map(item => {
        if (typeof item.detailJson == "string") {
          item.detailJson = JSON.parse(item.detailJson)
        }
      })
      console.log(list,'list')
      this.setData({
        list: list,
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
      });
    })
  },

  //下拉刷新
  onPullDownRefresh: function() {
    this.refreshData();
    wx.stopPullDownRefresh()
  },

  refreshData: function() {
    this.setData({
      pageNo: 1
    })
    this.getlist();
  },

  //触底时，加载更多
  onReachBottom: function() {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getlist();
    }
  },

  //查看详情 
  handleDetail: function(e) {
    wx.navigateTo({
      url: '/pages/shop_order/detail?id=' + e.currentTarget.dataset.id
    })
  },


  //申请退款
  refund: function(e) {
    wx.navigateTo({
      url: '/pages/shop_order/refund?id=' + e.currentTarget.dataset.id
    })
  },


  //评价
  assess: function(e) {
    wx.navigateTo({
      url: '/pages/shop_order/assess?id=' + e.currentTarget.dataset.id + '&itemid=' + e.currentTarget.dataset.itemid
    })
  },

  assess2: function (e) {
    wx.navigateTo({
      url: '/pages/shop_order/assess?id=' + e.currentTarget.dataset.id + '&itemid=' + e.currentTarget.dataset.itemid
        + '&assesstype=1'
    })
  },


  //评价列表
  assessList: function(e) {
    wx.navigateTo({
      url: '/pages/shop_order/assesslist?id=' + e.currentTarget.dataset.id
    })
  },

  //查看物流
  logistics: function(e) {
    wx.navigateTo({
      url: '/pages/logistics/index?id=' + e.currentTarget.dataset.id,
    })
  },

  // 取消订单
  cancelOdr: function(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      confirmText: '确定',
      cancelText: '取消',
      content: '确定取消该订单',
      success: function(res) {
        if (res.confirm) {
          _wxRequest.wxTonkenAjax({
            url: app.globalData.apiUrl + '/odr/protected/order/closeOrder',
            method: 'POST',
            contentType: 'application/x-www-form-urlencoded',
            data: {
              odrId: id
            }
          }, (res) => {
            wx.showToast({
              title: '取消成功',
            })
            that.getlist();
          })
        }
      }
    })
  },



  //确认收货
  take: function(e) {
    var id = e.currentTarget.dataset.id
    var that = this;
    wx.showModal({
      title: '提示',
      confirmText: '确定',
      cancelText: '取消',
      content: '是否确定收货',
      success: function(res) {
        if (res.confirm) {
          that.confirmDeliveryReq(id)
        }
      }
    })
  },

  //确认收货请求
  confirmDeliveryReq: function(id) {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/confirmDelivery',
      method: 'POST',
      data: {
        id
      }
    }, (res) => {
      wx.showToast({
        title: '收货成功',
      })
      that.getlist();
      // setTimeout(() => {
      //   wx.navigateTo({
      //     url: '/pages/order/assess?id='+id
      //   })
      // },1500)
    })
  },

  //微信支付
  wxPay: function(e) {
    var id = e.currentTarget.dataset.id
    var odrIdList = [id]
    var that = this;
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/wxPay',
      method: 'POST',
      data: odrIdList,
    }, function(res) {
      that.requestPayment(res, id)
    })
  },


  requestPayment: function(data, id) {
    var that = this;
    wx.requestPayment({
      'timeStamp': data.timeStamp,
      'nonceStr': data.nonceStr,
      'package': data.pkg,
      'signType': data.signType,
      'paySign': data.paySign,
      'success': function(res) {
        console.log(res, '支付成功参数')
        setTimeout(function() {
          wx.navigateTo({
            url: '/pages/settle/success?id=' + id,
          })
        }, 1000)
      },
      'fail': function(res) {
        console.log(res, '支付失败')
        wx.showToast({
          icon: 'none',
          title: '支付失败',
        })
      },
      'complete': function(res) {
        // that.payCallBack(data.outTradeNo)
      }
    })
  },

  //再次购买
  purchase: function(e) {
    var goodsOdrList = e.currentTarget.dataset.item
    var odrTotalPrice = e.currentTarget.dataset.price
    wx.setStorageSync('goodsOdrList', goodsOdrList)
    wx.setStorageSync('odrTotalPrice', odrTotalPrice)

    wx.navigateTo({
      url: '/pages/settle/index',
    })

  }




})